/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Yi Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/
 * @date:        2019/5/20 20:57
 * @email:       xiazhaoyang@live.com
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.example.common.spring;

import com.example.common.spring.config.CapPropertyPlaceholderConfigurer;
import com.example.common.constant.BasicConstants;
import com.example.common.spring.config.ConfigConstantProperties;
import com.example.common.spring.context.CapAppContext;
import com.example.common.util.RunnerMonitor;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.*;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Objects;
import java.util.Properties;

/**
 * <p>
 *
 * </p>
 *
 * @author Yi
 * @version v1.0.0
 * @date 2019-07-21 10:06
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2019-07-21
 * @modify reason: {方法名}:{原因}
 * ...
 */
@Configuration
public class SpringBaseBeanDefinition {

    /**
     * HTTP 请求类
     * @return
     */
    @Bean
    @Lazy
    public RestTemplate capRestTemplate(){
        ConfigConstantProperties constantProperties = CapAppContext.getBean(ConfigConstantProperties.class);
        return new RestTemplateBuilder()
                //时间参数配置
                .setConnectTimeout(constantProperties.getConnectTimeout())
                .setReadTimeout(constantProperties.getReadTimeout())
                .additionalMessageConverters(Sets.newHashSet(
                        new ByteArrayHttpMessageConverter(),
                        new MappingJackson2HttpMessageConverter(),
                        //处理文件流
                        new ResourceHttpMessageConverter(),
                        //设置UTF-8编码
                        new StringHttpMessageConverter(Charset.forName(BasicConstants.DEFAULT_ENCODING))))
                .requestFactory(HttpComponentsClientHttpRequestFactory.class)
                .build();
    }

    /**
     * 加载外部配置文件
     * @return
     */
    @Bean
    @Primary
    public CapPropertyPlaceholderConfigurer propertyConfigurer(){
        CapPropertyPlaceholderConfigurer configurer = new CapPropertyPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new FileSystemResource(Paths.get(RunnerMonitor.getEnvConfPath().getConfPath(), "config.yaml").toFile()));
        Properties props = new Properties();
        props.putAll(Objects.requireNonNull(yaml.getObject()));
        configurer.setProperties(props);
        configurer.setFileEncoding(BasicConstants.DEFAULT_ENCODING);
        return configurer;
    }
}
